home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / RCShaders / RCWindowLight.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  2.8 KB  |  90 lines

  1. /* Listing 16.21  Light shader simulating sunlight through a window*/
  2. /*
  3.  *  windowlight(): Cast patches of light as through a paned window.
  4.  */
  5. light 
  6. RCWindowLight(
  7.     point    from         = point "world" ( 0, 0, -20),
  8.         to        = point "world" (0, 0, 0),
  9.         center        = point "world" (0, 0, -4),
  10.         in        = point "world" (0, 0, 1),
  11.         up        = point "world" (0, 1, 0);
  12.     color    lightcolor     = color (1, .9, .6),
  13.         darkcolor     = color (.05, .2, .1); 
  14.     float     intensity    =  1,
  15.         xorder        =  2,    /* number of panes horizontally */
  16.         yorder        =  3,    /* number of panes vertically     */
  17.         panewidth    =  6,    /* horizontal size of a pane    */
  18.         paneheight    =  6,    /* vertical size of a pane    */
  19.         framewidth    =  1,    /* sash width between panes     */
  20.         fuzz    = .2;) /* transition region between pane and sash */
  21. {
  22.     uniform
  23.       point    in2,     /* normalized in                */
  24.         right,    /* unit vector perpendicular to in2 and up2 */
  25.         up2,    /* normalized up perpendicular to in        */
  26.         corner,    /* location of lower left corner of window  */
  27.                path;     /* direction of sunlight travel            */
  28.       point    PtoC,     /* vector from surface point to window corner */
  29.               PtoF;    /* vector from surface point to wall along path    */
  30.       float offset, modulus, yfract, xfract;
  31.  
  32.     /* Initialize the uniform variables */
  33.     path = (from - to);
  34.     in2 = normalize(in);
  35.     in2 *= sign(-path.in2);
  36.     right = up ^ in2;
  37.     up2 = normalize(in2 ^ right);
  38.     right = up2 ^ in2;
  39.     corner = center - right*xorder*panewidth/2 - 
  40.                 up2*yorder*paneheight/2;
  41.  
  42.     solar( -path, 0.0 ) {
  43.  
  44.     PtoC = corner - Ps;
  45.     if (path.PtoC <= 0) {         /* outdoors => full illumination */
  46.         xfract = yfract = 1;
  47.     } else {
  48.  
  49.     /*
  50.      * Make PtoF be a vector from the surface point to the wall 
  51.      *    by adjusting the length of the reflected vector path.
  52.      */
  53.         PtoF = path * (PtoC.in2)/(path.in2);
  54.  
  55.     /* 
  56.      * Calculate the vector from the corner to the intersection point, and
  57.      *    project it onto up2. This length is the vertical offset of the
  58.      *     intersection point within the window. 
  59.      */
  60.         offset = (PtoF - PtoC).up2;
  61.         modulus = mod(offset, paneheight);
  62.         if( offset > 0 && offset/paneheight < yorder ) { /* inside window */
  63.         if( modulus > (paneheight/2))
  64.             modulus = paneheight - modulus;                            /* symmetry in pane */
  65.         yfract = smoothstep(     /* include sash fuzz  */
  66.             (framewidth/2) - (fuzz/2),
  67.             (framewidth/2) + (fuzz/2),
  68.             modulus);
  69.         } else {
  70.         yfract = 0;
  71.         }
  72.  
  73.     /* Repeat for horizontal offset */
  74.         offset = (PtoF - PtoC).right;
  75.         modulus = mod(offset, panewidth);
  76.         if( offset > 0 && offset/panewidth < xorder ) {
  77.         if( modulus > (panewidth/2))
  78.             modulus = panewidth - modulus;
  79.         xfract = smoothstep( 
  80.             (framewidth/2) - (fuzz/2),
  81.             (framewidth/2) + (fuzz/2),
  82.             modulus);
  83.         } else {
  84.         xfract = 0;
  85.         }
  86.     }
  87.     Cl = intensity * mix( darkcolor, lightcolor, yfract*xfract);
  88.     }
  89. }
  90.